package com.ruoyi.warehouse.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sfproduct.domain.LwSfproduct;
import com.ruoyi.sfproduct.mapper.LwSfproductMapper;
import com.ruoyi.warehouse.domain.LwFpWarehouse;
import com.ruoyi.warehouse.domain.LwWarehousinginfo;
import com.ruoyi.warehouse.mapper.LwWarehousinginfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.warehouse.mapper.LwFpWarehouseWlMapper;
import com.ruoyi.warehouse.domain.LwFpWarehouseWl;
import com.ruoyi.warehouse.service.ILwFpWarehouseWlService;

/**
 * 网联成品仓库管理Service业务层处理
 * 
 * @author sll
 * @date 2023-12-14
 */
@Service
public class LwFpWarehouseWlServiceImpl implements ILwFpWarehouseWlService 
{
    @Autowired
    private LwFpWarehouseWlMapper lwFpWarehouseWlMapper;

    @Autowired
    private LwWarehousinginfoMapper LwWarehousinginfoMapper;

    @Autowired
    private LwSfproductMapper lwSfproductMapper;

    /**
     * 查询网联成品仓库管理
     * 
     * @param id 网联成品仓库管理主键
     * @return 网联成品仓库管理
     */
    @Override
    public LwFpWarehouseWl selectLwFpWarehouseWlById(Long id)
    {
        return lwFpWarehouseWlMapper.selectLwFpWarehouseWlById(id);
    }

    /**
     * 查询网联成品仓库管理列表
     * 
     * @param lwFpWarehouseWl 网联成品仓库管理
     * @return 网联成品仓库管理
     */
    @Override
    public List<LwFpWarehouseWl> selectLwFpWarehouseWlList(LwFpWarehouseWl lwFpWarehouseWl)
    {
        return lwFpWarehouseWlMapper.selectLwFpWarehouseWlList(lwFpWarehouseWl);
    }

    /**
     * 新增网联成品仓库管理
     * 
     * @param lwFpWarehouseWl 网联成品仓库管理
     * @return 结果
     */
    @Override
    public int insertLwFpWarehouseWl(LwFpWarehouseWl lwFpWarehouseWl)
    {

        LwFpWarehouse isEmpty = lwFpWarehouseWlMapper.selectLwFpWarehouseWlBySfproductId(lwFpWarehouseWl.getSfproductId());
        if (isEmpty == null){
            lwFpWarehouseWl.setRemark(null);

            LwSfproduct lwFproduct = lwSfproductMapper.selectLwSfproductBySfproductId(lwFpWarehouseWl.getSfproductId());
            lwFpWarehouseWl.setSfproductName(lwFproduct.getSfproductName());
            lwFpWarehouseWl.setModel(lwFproduct.getModel());
            lwFpWarehouseWl.setParameter(lwFproduct.getParameter());

            return lwFpWarehouseWlMapper.insertLwFpWarehouseWl(lwFpWarehouseWl);
        }else{
            // 添加半成品入库信息记录
            LwWarehousinginfo warehousinginfo = new LwWarehousinginfo();
            warehousinginfo.setSfproductName(lwFpWarehouseWl.getSfproductName());
            warehousinginfo.setModel(lwSfproductMapper.selectLwSfproductBySfproductId(lwFpWarehouseWl.getSfproductId()).getModel());
            warehousinginfo.setParameter(lwSfproductMapper.selectLwSfproductBySfproductId(lwFpWarehouseWl.getSfproductId()).getParameter());
            warehousinginfo.setTime(new Date());
            warehousinginfo.setEmployeeId(SecurityUtils.getUserId());
            warehousinginfo.setFlow(4L);
            warehousinginfo.setCompleted(lwFpWarehouseWl.getQuantity());
            warehousinginfo.setRemark(lwFpWarehouseWl.getRemark());
            lwFpWarehouseWlMapper.insertWarehousinginfo(warehousinginfo);


            Long quantity = isEmpty.getQuantity() + lwFpWarehouseWl.getQuantity();
            lwFpWarehouseWl.setQuantity(quantity);
            lwFpWarehouseWl.setId(isEmpty.getId());
            lwFpWarehouseWl.setRemark(isEmpty.getRemark());
            return lwFpWarehouseWlMapper.updateLwFpWarehouseWl(lwFpWarehouseWl);
        }



    }

    /**
     * 修改网联成品仓库管理
     * 
     * @param lwFpWarehouseWl 网联成品仓库管理
     * @return 结果
     */
    @Override
    public int updateLwFpWarehouseWl(LwFpWarehouseWl lwFpWarehouseWl)
    {
        return lwFpWarehouseWlMapper.updateLwFpWarehouseWl(lwFpWarehouseWl);
    }

    /**
     * 批量删除网联成品仓库管理
     * 
     * @param ids 需要删除的网联成品仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteLwFpWarehouseWlByIds(Long[] ids)
    {
        return lwFpWarehouseWlMapper.deleteLwFpWarehouseWlByIds(ids);
    }

    /**
     * 删除网联成品仓库管理信息
     * 
     * @param id 网联成品仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteLwFpWarehouseWlById(Long id)
    {
        return lwFpWarehouseWlMapper.deleteLwFpWarehouseWlById(id);
    }

    @Override
    public int updateFpWarehouseWlQuantity(LwFpWarehouseWl lwFpWarehouseWl) {
        if (lwFpWarehouseWl.getVariable() < 0){
            return -1;
        }
        if (lwFpWarehouseWl.getRemove() > lwFpWarehouseWl.getQuantity()){
            return -2;
        }
        if (lwFpWarehouseWl.getRemove() < 0){
            return -3;
        }
        // 新建仓库记录对象
        LwWarehousinginfo warehousingInfo = new LwWarehousinginfo();
        warehousingInfo.setSfproductName(lwFpWarehouseWl.getSfproductName());
        warehousingInfo.setModel(lwFpWarehouseWl.getModel());
        warehousingInfo.setParameter(lwFpWarehouseWl.getParameter());
        warehousingInfo.setTime(new Date());
        warehousingInfo.setEmployeeId(SecurityUtils.getUserId());
        warehousingInfo.setRemark(lwFpWarehouseWl.getRemark());
        // 入库
        if (lwFpWarehouseWl.getVariable() > 0) {
            lwFpWarehouseWl.setQuantity(lwFpWarehouseWl.getQuantity() + lwFpWarehouseWl.getVariable());
            warehousingInfo.setFlow(4L);
            warehousingInfo.setCompleted(lwFpWarehouseWl.getVariable());
        }
        //出库
        if (lwFpWarehouseWl.getRemove() > 0){
            lwFpWarehouseWl.setQuantity(lwFpWarehouseWl.getQuantity() - lwFpWarehouseWl.getRemove());

            //outboundType 0正常出库 1样品出库 2换货出库
            String outboundType = lwFpWarehouseWl.getOutboundType();
            switch (outboundType) {
                // 5L正常出库 10L样品出库 9L换货出库
                case "0": warehousingInfo.setFlow(5L); // 正常出库
                    break;
                case "1": warehousingInfo.setFlow(10L); // 样品出库
                    break;
                case "2": warehousingInfo.setFlow(9L); // 换货出库
                    break;
                default:
                    // 处理未知的outboundType
                    break;
            }
            warehousingInfo.setCompleted(lwFpWarehouseWl.getRemove());

            warehousingInfo.setClient(lwFpWarehouseWl.getClient());
            warehousingInfo.setSalesman(lwFpWarehouseWl.getSalesman());
        }
        LwWarehousinginfoMapper.insertLwWarehousinginfo(warehousingInfo);

        LwFpWarehouseWl fpWarehouseWl = lwFpWarehouseWlMapper.selectLwFpWarehouseWlById(lwFpWarehouseWl.getId());
        lwFpWarehouseWl.setRemark(fpWarehouseWl.getRemark());
        return lwFpWarehouseWlMapper.updateLwFpWarehouseWl(lwFpWarehouseWl);
    }
}
